package com.github.smartheye.fabric.common.crypto;

import java.io.StringReader;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

public class SecurityManager {

    static {
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
    }

    public static final String getProviderName() {
        return BouncyCastleProvider.PROVIDER_NAME;
    }

    public static final Provider getProvider() {
        return Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
    }

    public static final JcaPEMKeyConverter newJcaPEMKeyConverter() {
        JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter();
        keyConverter.setProvider(getProviderName());
        return keyConverter;
    }

    public static final SecureRandom getSecureRandom() throws NoSuchAlgorithmException {
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        return sr;
    }
    
    public static final PEMParser newPEMParser(String content){
        return new PEMParser(new StringReader(content));
    }
}
